const express = require("express");
const router = express.Router();
const expressJoi = require("@escook/express-joi")
const userInfoHandler = require('../router_handler/userInfo')
const { reg_changePassword_schema, reg_changeName_schema, reg_changeSex_schema, reg_changeEmail_schema, reg_addProductManager_schema, reg_getProductManager_schema, reg_modifyProductManager_schema } = require("../schema/userInfo")

// 导入解析 formdata 格式表单数据的包
const multer = require('multer')
var storage = multer.diskStorage({
    //设置上传后文件路径，uploads文件夹会自动创建。
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '../public/uploads'))
    },
    /**
     * 给上传文件重命名，获取添加后缀名
     * 参数名 + 文件名 + 时间戳
     * */
    filename: function (req, file, cb) {
        var fileFormat = (file.originalname).split(".");

        //  返回文件名
        cb(null, file.fieldname + '_' + fileFormat[0] + '_' + Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
});
// 导入处理路径的核心模块
const path = require('path')
// 创建 multer 的实例对象，通过 dest 属性指定文件的存放路径
// const upload = multer({ dest: path.join(__dirname, '../public/uploads') })
const upload = multer({
    storage: storage
});
router.post('/uploadAvatar', upload.single('imageUrl'), userInfoHandler.uploadAvatar)

router.get('/info', userInfoHandler.info)

router.post('/changePassword', expressJoi(reg_changePassword_schema), userInfoHandler.changePassword);

router.post('/changeName', expressJoi(reg_changeName_schema), userInfoHandler.changeName);

router.post('/changeSex', expressJoi(reg_changeSex_schema), userInfoHandler.changeSex);

router.post('/changeEmail', expressJoi(reg_changeEmail_schema), userInfoHandler.changeEmail);

router.post('/addProductManager', expressJoi(reg_addProductManager_schema), userInfoHandler.addProductManager);

router.post('/getProductManager',  userInfoHandler.getProductManager);
router.get('/deleteProductManager', userInfoHandler.deleteProductManager);
router.post('/modifyProductManager',expressJoi(reg_modifyProductManager_schema), userInfoHandler.modifyProductManager);

router.post('/getUser', userInfoHandler.getUser);
router.get('/deletUser', userInfoHandler.deletUser);
router.post('/empowerment', userInfoHandler.empowerment);

router.post('/changeType', userInfoHandler.changeType);
module.exports = router